<template>
  <div>
    <!-- 顶部搜索框 -->
    <van-nav-bar
      :border="false"
      @click-right="onSearch"
      @click-left="onClickLeft"
        right-text="搜索"
    >
    <template #left>
    <van-icon name="arrow-left"  color="#FF7F24"/>
    </template>
      <template #title>
        <van-search
          v-model="value"
          :left-icon="require('../assets/baking_log/search.png')"
          icon-size="12"
          placeholder="搜索食谱或食材"
          @search="onSearch"
        >
        </van-search>
        </template>
    </van-nav-bar>
    <div class="main">
      <van-row v-if="historyList.length != 0">
        <van-col span="19" offset="1">历史搜索</van-col>
        <van-col span="2" offset="1" @click="empty">清空</van-col>
      </van-row>
      <div class="historysearch">
        <span
          @click="goSearchDetail(item)"
          v-for="(item, index) in historyList"
          :key="index"
          >{{ item }}</span
        >
      </div>
      <van-row>
        <van-col span="23" offset="1">热门搜索</van-col>
      </van-row>
      <div class="topsearch">
        <span>吐司</span>
        <span>披萨</span>
        <span>蛋卷</span>
        <span>奶冻</span>
        <span>油条</span>
        <span>曲奇</span>
        <span>奶酪包</span>
        <span>冰淇淋</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      value: "",
      historyList: [], //历史搜索记录，存本地
    };
  },
  mounted() {
    if (JSON.parse(localStorage.getItem("historyList"))) {
      this.historyList = JSON.parse(localStorage.getItem("historyList"));
    }
  },
  methods: {
    onClickLeft() {
      return this.$router.go(-1);
    },
    onSearch() {
      // 如果为空，则弹出提示信息
      if (this.value.trim() == "") {
        this.$toast("请输入搜索内容");
        return 0;
      } else {
        // 没有搜索记录，把搜索值push进数组首位，存入本地
        if (!this.historyList.includes(this.value)) {
          this.historyList.unshift(this.value);
          localStorage.setItem("historyList", JSON.stringify(this.historyList));
        } else {
          // 有搜索记录，就删除之前的旧纪录，将新搜索值重新push到数组首位
          let i = this.historyList.indexOf(this.value);
          this.historyList.splice(i, 1);
          this.historyList.unshift(this.value);
          localStorage.setItem("historyList", JSON.stringify(this.historyList));
        }
        // 跳转到搜索结果页
        if (this.$route.path == `/recipe_list?rname${this.value}`) return;
        this.$router.push({
          path: `/recipe_list`,
          query: {
            rname: this.value,
          },
        });
      }
    },
    // 点击搜索记录，跳转搜索结果页
    goSearchDetail(title) {
      this.$router.push({
        path: `/recipe_list`,
        query: {
          rname: title,
        },
      });
    },
    // 清空历史搜索记录
    empty() {
      localStorage.removeItem("historyList");
      this.historyList = [];
    },
  },
};
</script>

<style scoped>
:deep(.van-nav-bar__text) {
  color: #FF7F24;
}
:deep(.van-nav-bar .van-icon) {
  color: #333;
}
:deep(.van-nav-bar) {
  overflow: hidden;
}
:deep(.van-nav-bar__title) {
  max-width: 90%;
  padding-right: 10px;
}
:deep(.van-search) {
  width: 80vw;
}
:deep(.van-icon__image) {
  margin-top: 4px;
}
:deep(.van-row) {
  padding: 20px 0 5px;
}
:deep(.van-col) {
  color: #a2a2a2;
  font-size: 15px;
}
.main {
  width: 100vw;
  height: 100vh;
  background-color: #fff;
}
.topsearch,
.historysearch {
  display: flex;
  flex-wrap: wrap;
  padding-left: 10px;
  user-select: none;
}
.topsearch > span,
.historysearch > span {
  background-color: rgba(0, 0, 0, 0.05);
  padding: 5px;
  margin: 5px;
  border-radius: 10px;
  color: #4a4945;
}
</style>

<style lang="scss" scoped></style>
